"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 导入导出、备份恢复工具支持M*兼容性
Case Name   : 存储过程中调用json函数，使用gs_dump导出后再执行导入
Create At   : 2023.05.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.兼容b库创建测试用户并授权
    3.兼容b库创建存储过程，存储体使用json函数
    4.使用gs_dump工具导出表为纯文本格式
    5.导入之前导出的数据到新库
    6.连接导入库查询
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GsDump0021(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_dump_0021"
        self.db_name_01 = "db_dump_0021_01"
        self.pro_name = "pro_dump_0021"
        self.user_name = "user_dump0021"
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'dump0021')

    def test_server_tools(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1: 创建兼容b库 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} encoding 'UTF8' " \
                  f"dbcompatibility 'b';" \
                  f"drop database if exists {self.db_name_01};" \
                  f"create database {self.db_name_01} encoding 'UTF8' " \
                  f"dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertEqual(2, sql_result.count(
            self.constant.CREATE_DATABASE_SUCCESS), '执行失败:' + text)

        text = '---step2: 兼容b库创建测试用户并授权 expect: 成功---'
        self.log.info(text)
        sql_cmd = f'''drop user if exists {self.user_name} cascade;
        create user {self.user_name} password '{macro.PASSWD_INITIAL}';\
        grant proxy on current_user() to {self.user_name};'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_result,
                      '执行失败:' + text)
        self.assertIn(self.constant.GRANT_SUCCESS_MSG, sql_result,
                      '执行失败:' + text)

        text = '--step3: 兼容b库创建存储过程，存储体使用json函数 expect: 成功----'
        self.log.info(text)
        sql_cmd = f'''drop procedure if exists {self.pro_name};
        create definer ={self.user_name} procedure {self.pro_name}(
            json1 out json,
            json2 out json,
            json3 out boolean,
            json4 out json,
            json5 out text,
            json6 out json,
            json7 out text,
            json8 out json,
            json9 out json,
            json10 out integer,
            json11 out integer,
            json12 out text,
            json13 out integer
            )
            is
            begin
                 select json_array(1,'a','b',true,null) into json1;
                 select json_quote('gauss') into json2;
                 select json_contains('[1,2,3,4,5]','[3,5]') into json3;
                 select json_extract(cast('true' as json), '\$')into json4;
                 select json_unquote('"dajifa\\tIMIDF"') into json5;
                 select json_keys(cast('true' as json)) into json6;
                 select json_search('\"abc\"','one','abc',true) into json7;
                 select json_array_append('\"a\"', '\$', 'add') into json8;
                 select json_array_insert(cast('true' as json),'\$[0]','add') \
                 into json9;
                 select json_depth(cast('true' as json)) into json10;
                 select json_length('null') into json11;
                 select json_type('\"aa\"') into json12;
                 select json_storage_size('0') into json13;
            end;'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, sql_result,
                      '执行失败:' + text)

        text = '----step4: 使用gs_dump工具导出表为纯文本格式 expect: 成功----'
        self.log.info(text)
        gs_dump_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"gs_dump " \
                      f"-p {self.dbuser_node.db_port} " \
                      f"{self.db_name} -F p " \
                      f"-f {self.dump_path}"
        self.log.info(gs_dump_cmd)
        dump_msg = self.dbuser_node.sh(gs_dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(f"dump database {self.db_name} successfully",
                      dump_msg, '执行失败:' + text)

        text = '----step5: 导入之前导出的数据到新库 expect: 成功----'
        self.log.info(text)
        restore_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"gsql -p {self.dbuser_node.db_port} " \
                      f"{self.db_name_01} -r -f {self.dump_path}"
        self.log.info(restore_cmd)
        restore_msg = self.dbuser_node.sh(restore_cmd).result()
        self.log.info(restore_msg)
        self.assertEqual(1, restore_msg.count('CREATE PROCEDURE'),
                         '执行失败:' + text)

        text = '-step6: 连接导入库查询 expect: 成功--'
        self.log.info(text)
        sql_cmd = f"show create procedure {self.pro_name};"

        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        self.assertIn(f'CREATE DEFINER = {self.user_name} PROCEDURE',
                      sql_result, '执行失败:' + text)
        self.assertIn('(1 row)', sql_result, '执行失败:' + text)

    def tearDown(self):
        text = '---step7: 清理环境 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name_01};" \
                  f"drop user if exists {self.user_name} cascade;"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        rm_cmd = f"rm -rf {self.dump_path}"
        self.log.info(rm_cmd)
        rm_msg = self.dbuser_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_result,
                      '执行失败:' + text)
        self.assertEqual(2,
                         sql_result.count(self.constant.DROP_DATABASE_SUCCESS))
        self.assertEqual('', rm_msg)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
